var app = getApp();
// Page({
//   data: {
//     imgUrls: [
//       'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
//       'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
//       'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
//     ],
//     indicatorDots: true,
//     autoplay: true,
//     interval: 2000,
//     duration: 1000
//   },
  
// })


function getRandomColor() {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
} 

Page({
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  inputValue: '',
  data: {
    imgUrls: [
      // 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      // 'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    controls:"true",
    vedio:'',
    classify:[
      // { img:"https://m.360buyimg.com/mobilecms/jfs/t5965/339/3633548361/13799/cd4d0416/5954cf81N3294a71c.png",span:"全部商品"},
      // { img:"https://m.360buyimg.com/mobilecms/jfs/t5965/339/3633548361/13799/cd4d0416/5954cf81N3294a71c.png",span:"当季热卖"},
      // { img:"https://m.360buyimg.com/mobilecms/jfs/t5965/339/3633548361/13799/cd4d0416/5954cf81N3294a71c.png",span:"联系客服"},
      // { img:"https://m.360buyimg.com/mobilecms/jfs/t5965/339/3633548361/13799/cd4d0416/5954cf81N3294a71c.png",span:"品牌文化"}
    ],
    show:[
      // { img: "https://m.360buyimg.com/mobilecms/s357x357_jfs/t2185/5/2707056436/356808/2be83568/56ea3a0fN12d87b59.jpg!q50.jpg", name: "01百搭王牌组合",price:277.00},
      // { img: "https://m.360buyimg.com/mobilecms/s357x357_jfs/t2185/5/2707056436/356808/2be83568/56ea3a0fN12d87b59.jpg!q50.jpg", name: "02祛痘嫩肤组合", price: 547.00},
      // { img: "https://m.360buyimg.com/mobilecms/s357x357_jfs/t2185/5/2707056436/356808/2be83568/56ea3a0fN12d87b59.jpg!q50.jpg", name: "03水嫩嫩亮肤组合", price: 910.00},
      // { img: "https://m.360buyimg.com/mobilecms/s357x357_jfs/t2185/5/2707056436/356808/2be83568/56ea3a0fN12d87b59.jpg!q50.jpg", name: "水嫩嫩补水组合", price: 917.00 }
    ],
    page: 0,
   
  },
  onLoad: function (options) {
    var that = this
    
    //获取分享页面的参数
    var dcesuid = decodeURIComponent(options.scene).split("=");//获取二维码分享的share_uid
    let uid = options.share_userId ? options.share_userId : dcesuid[1];//判断是二维码or链接分享的share_uid
    
    console.log(uid + '.uid');
    console.log(dcesuid + 'scene');
    
    let gs_share_userId = wx.getStorageSync('share_userId') || ''
    console.log(gs_share_userId + '.73');
    //如果是分享页面则保存分享的用户ID保存到storage
    if(uid && !gs_share_userId){
      try {
        wx.setStorageSync('share_userId', uid)
        // wx.showToast({
        //   title: '保存成功',
        //   icon: 'success',
        //   duration: 2000
        // })
      } catch (e) {
        wx.showToast({
          title: '保存失败',
          icon: 'success',
          duration: 2000
        })
      }
    }
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Index/index',
      method: 'post',
      data: {},
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          classify: res.data.last,
          imgUrls: res.data.ggtop,
          show: res.data.prolist,
          first: res.data.first,
          vedio: res.data.vedio,
        });
      }
    })
    app.getUserInfo(function (userInfo) {
      //更新数据  
      that.setData({
        userInfo: userInfo
      })
    })
  },
  searchProductData: function () {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Search/searches',
      method: 'post',
      data: {
        keyword: that.data.searchValue,
        uid: app.d.userId,
        page: that.data.page,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        var data = res.data.pro;
        that.setData({
          show: res.data.pro,
        });
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    });
  },
  doSearch: function () {
    var searchKey = this.data.searchValue;
    this.searchProductData();
  },
  searchValueInput: function (e) {
    var value = e.detail.value;
    this.setData({
      searchValue: value,
    });
    // if (!value && this.data.productData.length == 0) {
    //   this.setData({
    //     hotKeyShow: true,
    //     historyKeyShow: true,
    //   });
    // }
  },
    /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '万棵树百汇商城',
      path: '/pages/index/index?share_userId=' + app.d.userId,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
  
  
})